using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using WMPLib;

using AxWMPLib;
namespace Parsifal
{
    public class ManageMediaPlayer
    {
        public delegate void OnMediaPlayerChangeCurrentPlaylistItem(IWMPMedia aMedia);
        public delegate void OnMediaPlayerStatePlayChanged(WMPPlayState aState);

        public event OnMediaPlayerChangeCurrentPlaylistItem _OnMediaPlayerChangeCurrentPlaylistItem;
        public event OnMediaPlayerStatePlayChanged _OnMediaPlayerStatePlayChanged;


        private Utility iUtility;
        private string mLocalUrl = "http://127.0.0.1:8080";
        private string mExternUrl;

        private AxWindowsMediaPlayer iPlayer;

        private WMPLib.WMPPlayState mStatePlayer = WMPPlayState.wmppsUndefined;


        public enum MNG_MEDIA_PLAYER_URL_TYPE
        {
            MNG_MEDIA_PLAYER_URL_LOCAL,
            MNG_MEDIA_PLAYER_URL_EXTERN,
            MNG_MEDIA_PLAYER_PLAYPLAYLIST
        }


        public WMPLib.WMPPlayState StatePlayer
        {
            get { return mStatePlayer; }
        }


        public ManageMediaPlayer(Utility aUtility,AxWindowsMediaPlayer aPlayer)
        {
            iUtility = aUtility;

            iPlayer = aPlayer;
            iPlayer.CurrentItemChange +=new AxWMPLib._WMPOCXEvents_CurrentItemChangeEventHandler(iPlayer_CurrentItemChange);
            iPlayer.PlayStateChange +=new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(iPlayer_PlayStateChange);
            iPlayer.PositionChange +=new AxWMPLib._WMPOCXEvents_PositionChangeEventHandler(iPlayer_PositionChange);
            iPlayer.MediaChange +=new AxWMPLib._WMPOCXEvents_MediaChangeEventHandler(iPlayer_MediaChange);


            
        }


        public void StartMediaPlayer(MNG_MEDIA_PLAYER_URL_TYPE aType)
        {
            switch (aType)
            {
                case MNG_MEDIA_PLAYER_URL_TYPE.MNG_MEDIA_PLAYER_URL_EXTERN:
                    {
                    }
                    break;
                case MNG_MEDIA_PLAYER_URL_TYPE.MNG_MEDIA_PLAYER_URL_LOCAL:
                    {
                        //iPlayer.URL = mLocalUrl;
                        iPlayer.URL = mLocalUrl;
                        
                    }
                    break;
                case MNG_MEDIA_PLAYER_URL_TYPE.MNG_MEDIA_PLAYER_PLAYPLAYLIST:
                    {
                        iPlayer.Ctlcontrols.play();
                    }
                    break;
                default:
                    break;
            }
        }

        public void SwitchMediaItem(PlaylistItem drag, PlaylistItem inFront)
        {
            IWMPPlaylist iP = iPlayer.currentPlaylist;

            int iMediaDrag = GetMediaFromSourceUrl(drag.PhysicalPath);
            int iMediaInFront = GetMediaFromSourceUrl(inFront.PhysicalPath);



            iP.moveItem(iMediaDrag, iMediaInFront);

            
        }

        public void NextSong()
        {
            iPlayer.Ctlcontrols.next();
        }

        public void PreviousSong()
        {
            iPlayer.Ctlcontrols.previous();
        }

        public void ActiveIndexSong(PlaylistItem aItem)
        {//aggiornato
            try
            {
                IWMPPlaylist iP = iPlayer.currentPlaylist;
                for (int i = 0; i < iP.count; i++)
                {
                    IWMPMedia iMedia = iP.get_Item(i);
                    if (iMedia.sourceURL == aItem.PhysicalPath)
                    {
                        //iPlayer.currentPlaylist = iP;
                        iPlayer.Ctlcontrols.playItem(iMedia);
                    }
                }

            }
            catch (Exception err)
            {
                
                throw new Exception("Errore ne Manage Media Player...su song selezionata : errore : " + err);
            }
        }

        private int GetMediaFromSourceUrl(string sourceUrl)
        {
            //aggiornato
            IWMPMedia iMedia = null;
            int res = -1;
            try
            {
                

                IWMPPlaylist iP = iPlayer.currentPlaylist;
                for (int i = 0; i < iP.count; i++)
                {
                    iMedia = iP.get_Item(i);
                    if (iMedia.sourceURL == sourceUrl)
                    {
                        //iPlayer.currentPlaylist = iP;
                        //iPlayer.Ctlcontrols.playItem(iMedia);
                        res = i;
                    }
                }


            }
            catch (Exception err)
            {


                throw new Exception("Errore ne Manage Media Player...su song selezionata : errore : " + err);
            }

            return res;
            
        }

        public void StopMediaPlayer()
        {
            iPlayer.Ctlcontrols.stop();
        }


        #region Override Method Player

        private void iPlayer_MediaChange(Object sender, _WMPOCXEvents_MediaChangeEvent e)
        {
            IWMPMedia iMedia=(IWMPMedia)e.item;
            if (_OnMediaPlayerChangeCurrentPlaylistItem != null)
            {
                _OnMediaPlayerChangeCurrentPlaylistItem(iMedia);
            }
        }

        private void iPlayer_PositionChange(Object sender, _WMPOCXEvents_PositionChangeEvent e)
        {
            
        }

        private void iPlayer_CurrentItemChange(Object sender, _WMPOCXEvents_CurrentItemChangeEvent e)
        {

            IWMPMedia iMedia =(IWMPMedia) e.pdispMedia;

           
        }

        private void iPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            WMPPlayState newState = (WMPPlayState)e.newState;

            mStatePlayer = newState;

            if (_OnMediaPlayerStatePlayChanged != null)
            {
                _OnMediaPlayerStatePlayChanged(newState);
            }
        }

        #endregion

        #region " gestione playlist "

        public void AddItemToPlaylistMediaPlayer(PlaylistItem aItem)
        {
            IWMPPlaylist iPlaylist = iPlayer.currentPlaylist;
            IWMPMedia aMedia = iPlayer.newMedia(aItem.PhysicalPath);
            iPlaylist.appendItem(aMedia);

        }

        public void AddItemToPlaylistMediaPlayerEx(PlaylistItem aItem,int inFrontOf)
        {
            IWMPPlaylist iPlaylist = iPlayer.currentPlaylist;
            IWMPMedia aMedia = iPlayer.newMedia(aItem.PhysicalPath);
            iPlaylist.insertItem(inFrontOf, aMedia);

        }

        public void UpdatePlaylistMediaPlayer(ref ListBox aList)
        {
            aList.Items.Clear();

            IWMPPlaylist iPlaylist = iPlayer.currentPlaylist;
            IWMPMedia aMedia;

            for (int i = 0; i < iPlaylist.count; i++)
            {
                aMedia = iPlaylist.get_Item(i);
                aList.Items.Add(aMedia.name);

            }
        }

        
        public void RemovePlaylistMediaPlayer(int index,PlaylistItem aItem)
        {
            IWMPPlaylist iPlaylist = iPlayer.currentPlaylist;
            IWMPMedia aMedia;
            for (int i = 0; i < iPlaylist.count; i++)
            {
                aMedia = iPlaylist.get_Item(i);
                if (aItem.PhysicalPath == aMedia.sourceURL)
                {
                    iPlaylist.removeItem(aMedia);
                    return;
                }

            }
            
        }
        #endregion


        }

    
}
